<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Login extends Controller
{

    public function index()
    {
        return view('login');
    }

    public function login(Request $request){

        $data = $request->param();

        // 登陆验证
        $rule = [
            'username' => 'require|token',
            'password' => 'require',
            'verify' => 'require'
        ];

        $msg = [
            'username.require' => '用户名不得为空',
            'username.token' => '重复提交',
            'password.require' => '用户名不得为空',
            'verify.require' => '用户名不得为空',
        ];

        $validate = new \think\Validate($rule,$msg);

        if(!$validate->check($data)){
            $error = $validate->getError();
            $this->error($error);
            die;
        }

        $username = $data['username'];
        $password = md5($data['password']);

        $data = \app\admin\model\User::where('username',$username)->where('password',$password)->find();

        if($data){
            $sess = new \think\Session();

            $sess::set("msg_info",$data);

            $this->success("登陆成功",'admin/Index/index');
        }else{
            $this->error("用户名或密码错误，请重新登陆",'admin/Login/index');
        }

    }
}
